package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <h3>leyou-project</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-05-26 13:57
 **/
@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {

    private SpecParamService paramService;

    public SpecGroupServiceImpl(SpecParamService paramService) {
        this.paramService = paramService;
    }

    @Override
    public List<SpecGroupDTO> querySpecGroupDetailByCategoryId(Long id) {
        /**
         *      根据id查找规格组
         *      再根据规格组的id查找详细规格参数
         */
        List<SpecGroup> specGroups = query().eq("category_id", id).list();

        List<SpecGroupDTO> groupDTOList = SpecGroupDTO.convertEntityList(specGroups);

        List<SpecParam> specParamList = paramService.query().eq("category_id", id).list();
        List<SpecParamDTO> paramDTOList = SpecParamDTO.convertEntityList(specParamList);
        Map<Long, List<SpecParamDTO>> listMap = paramDTOList.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        groupDTOList.forEach(group->group.setParams(listMap.get(group.getId())));
        return groupDTOList;
    }
}
